<!--
 * @Description: 二分查找
 * @FilePath: \算法\查找\001二分查找.html
--> 
<script>

function binary_search(arr, target) {
    let res = -1;    // 目标元素的下标
    let l_point = 0,
        r_point = arr.length - 1;   // 在[l...r]之间存在目标元素

    while(l_point <= r_point) {
        let middle = parseInt((l_point+r_point)/2);   // 中间索引
        if(target === arr[middle]) return middle;
        if(target < arr[middle]) {
            r_point = middle - 1;
        } else {
            l_point = middle + 1;
        }
    }

    return res;
}

var arr = [1,2,3,4,5,6,7,8,9,10,11,23,44,86];
var result = binary_search(arr,866);
console.log(result); // 9 返回目标元素的索引值 

</script>